package zhao.tx.eecs.zhao;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import zhao.tx.eecs.R;
import zhao.tx.eecs.common.CommonActivity;

public class MapFrm extends CommonActivity {
	private MapView mMapView = null; 
	private BaiduMap mBaiduMap;
	private static final String TAG =  "aaa" ;  
	// 定位相关
	LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	private LocationMode mCurrentMode;
	BitmapDescriptor mCurrentMarker;


	// UI相关
	OnCheckedChangeListener radioButtonListener;
	Button requestLocButton;
	boolean isFirstLoc = true;// 是否首次定位
	@Override
    public void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);
      //在使用SDK各组件之前初始化context信息，传入ApplicationContext  
        //注意该方法要再setContentView方法之前实现  
        SDKInitializer.initialize(getApplicationContext()); 
        setContentView( R.layout.mapfrm);
        menu1 = (LinearLayout) findViewById(R.id.menu1);
        menu1.setOnTouchListener( view1);	
        menu2 = (LinearLayout) findViewById(R.id.menu2);
        menu2.setOnTouchListener( view2);	
        menu3 = (LinearLayout) findViewById(R.id.menu3);
        menu3.setOnTouchListener( view3);	
        
        View xhx1=(View)findViewById(R.id.xhx1);
        View xhx2=(View)findViewById(R.id.xhx2);
        View xhx3=(View)findViewById(R.id.xhx3);
        xhx2.setVisibility( View.VISIBLE);
		xhx1.setVisibility(View.GONE);
		xhx3.setVisibility(View.GONE);
        mMapView=(MapView)findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();  
        //普通地图  
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);  
        //卫星地图  
        //mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
     // 开启定位图层
     		mBaiduMap.setMyLocationEnabled(true);
     		// 定位初始化
     		mLocClient = new LocationClient(this);
     		mLocClient.registerLocationListener(myListener);
     		LocationClientOption option = new LocationClientOption();
     		option.setOpenGps(true);// 打开gps
     		option.setCoorType("bd09ll"); // 设置坐标类型
     		option.setScanSpan(2000);
     		mLocClient.setLocOption(option);
     		mLocClient.start();
     	}

     	/**
     	 * 定位SDK监听函数
     	 */
     	public class MyLocationListenner implements BDLocationListener {

			@Override
     		public void onReceiveLocation(BDLocation location) {
     			// map view 销毁后不在处理新接收的位置
     			if (location == null || mMapView == null){
     				Log.e(TAG, "ERROR");
     				return;	
     			}
     				
     			MyLocationData locData = new MyLocationData.Builder()
     					.accuracy(location.getRadius())
     					// 此处设置开发者获取到的方向信息，顺时针0-360
     					.direction(100).latitude(location.getLatitude())
     					.longitude(location.getLongitude()).build();
     			mBaiduMap.setMyLocationData(locData);
     			if (isFirstLoc) {
     				isFirstLoc = false;
     				LatLng ll = new LatLng(location.getLatitude(),
     						location.getLongitude());
     				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
     				
     				 StringBuffer sb = new StringBuffer(256);
     			      sb.append("time : ");
     			      sb.append(location.getTime());
     			      sb.append("\nerror code : ");
     			      sb.append(location.getLocType());
     			      sb.append("\nlatitude : ");
     			      sb.append(location.getLatitude());
     			      sb.append("\nlontitude : ");
     			      sb.append(location.getLongitude());
     			      sb.append("\nradius : ");
     			      sb.append(location.getRadius());
     			      if (location.getLocType() == BDLocation.TypeGpsLocation){
     			           sb.append("\nspeed : ");
     			           sb.append(location.getSpeed());
     			           sb.append("\nsatellite : ");
     			           sb.append(location.getSatelliteNumber());
     			       } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
     			           sb.append("\naddr : ");
     			           sb.append(location.getAddrStr());
     			        } 
     			      
     			    Log.v(TAG, sb.toString());
     				//tx.common.DialogDemo.builder(MapFrm.this, "信息提示", sb.toString());
     				mBaiduMap.animateMapStatus(u);
     			}
			}

     		public void onReceivePoi(BDLocation poiLocation) {
     		}
     	}
	
	public void btn_OK_click(View v) {			 
		//定义Maker坐标点  
		LatLng point = new LatLng(26.663175, 115.89400244); 
		//构建Marker图标  
		BitmapDescriptor bitmap = BitmapDescriptorFactory  
		    .fromResource(R.drawable.poptip);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option = new MarkerOptions()  
		    .position(point)  
		    .icon(bitmap);  
		//在地图上添加Marker，并显示  
		mBaiduMap.clear();
		mBaiduMap.addOverlay(option);
		MapStatusUpdate u = MapStatusUpdateFactory
				.newLatLng(point);
		mBaiduMap.setMapStatus(u);
	}

    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理  
        mMapView.onDestroy();  
    }
    
    @Override  
    protected void onResume() {  
        super.onResume();  
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理  
        mMapView.onResume();  
    }
    
    @Override  
    protected void onPause() {  

        super.onPause();  
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理  
        mMapView.onPause();  
    }  

    public void btn_shebei_click(View v) {	
		 
		 Intent intent = new Intent();		 
		 intent.setClass(getApplicationContext(), PD_Shaomiao.class);
		 startActivity(intent);
	}
}
    

